package com.cbgolf.oa.model;

import android.support.v4.util.ArrayMap;
import com.amap.api.maps.model.LatLng;
import com.cbgolf.oa.base.BaseModel;
import com.cbgolf.oa.contract.IPlayerCaddieCarDetailsContract.IPlayerCaddieCarDetailsModel;
import com.cbgolf.oa.entity.LatLon;
import com.cbgolf.oa.entity.ParkBean;
import com.cbgolf.oa.entity.ParkFair;
import com.cbgolf.oa.entity.PlayerBean;
import com.cbgolf.oa.entity.StatisticsBean;
import com.cbgolf.oa.entity.TeeTimeBean;
import com.cbgolf.oa.event.EventPark;
import com.cbgolf.oa.manager.DataManager;
import com.cbgolf.oa.net.NetCallBack;
import com.cbgolf.oa.net.Web;
import com.cbgolf.oa.net.WebAPI;
import com.cbgolf.oa.util.CollectionUtil;
import com.cbgolf.oa.util.MapUtil;
import com.cbgolf.oa.util.Util;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import org.greenrobot.eventbus.EventBus;

/**
 * @author lq
 */
public class PlayerCaddieCarDetailsModelImp extends BaseModel implements
    IPlayerCaddieCarDetailsModel {


  @Override
  public void requestData(int dataType, int requestEvent, long teetimeDate) {
    String url = WebAPI.statistics_park_worker_caddie_get;
    Map<String, String> map = new ArrayMap<>();
    switch (dataType) {
      case DataManager.STATISTICS_PLAYER_IN_PARK:
        url = WebAPI.statistics_park_worker_caddie_get;
        break;
      case DataManager.STATISTICS_CADDIE_IN_PARK:
        url = WebAPI.statistics_park_caddie_get;
        break;
      case DataManager.STATISTICS_PLAYER_REG_DETAILS:
        url = WebAPI.statistics_park_player_reg_details_get;
        map.put("teeTimeDate", String.valueOf(teetimeDate));
        break;
      case DataManager.STATISTICS_CAR_USING:
        url = WebAPI.statistics_park_car_get;
        break;
      default:
        break;
    }
    Web.getOK(url, map,
        new NetCallBack(response -> EventBus.getDefault().post(new EventPark.Builder()
            .setTarget(dataType)
            .setRequestEvent(requestEvent)
            .setParkBean((ParkBean) getBean(response, ParkBean.class))
            .setListTeetime(getList(response, TeeTimeBean.class))
            .setListStatistics(getList(response, StatisticsBean.class))
            .setError(errorMsg, errorCode)
            .build())));
  }

  @Override
  public void filterPlayerData(List<LatLon> listCaddie, List<ParkFair> listAll) {
    listInWayBack.clear();
    if (Util.listIsNull(listCaddie) || Util.listIsNull(listAll)) {
      return;
    }
    ThreadFactory factory = r -> new Thread(r);
    ExecutorService pool = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS,
        new LinkedBlockingQueue<>(1024), factory, new ThreadPoolExecutor.AbortPolicy());
    pool.execute(() -> {
      filterDataBegin2(listCaddie, listAll);
    });
    pool.shutdown();

  }

  private List<PlayerBean> listInWayBack = new ArrayList<>();

  private void filterDataBegin(List<LatLon> listCaddie, List<ParkFair> listAll) {
    List<LatLng> listCaddieLatlng = MapUtil.getCovertList(listCaddie);
    List<List<LatLng>> listAllLatlng = MapUtil.geListAllLatlng(listAll);
    List<LatLon> listCaddieWithPlayer;
    List<List<LatLon>> listData;
    Map<String, List<List<LatLon>>> map = new ArrayMap<>();
    String wayName;
    for (int i = 0; i < listCaddie.size(); i++) {
      listCaddieWithPlayer = new ArrayList<>();
      for (int j = 0; j < listAll.size(); j++) {
        if (MapUtil.isInRange(listCaddieLatlng.get(i), listAllLatlng.get(j))) {
          wayName = listAll.get(j).fairwayName;
          listCaddieWithPlayer.add(listCaddie.get(i));
          if (!Util.listIsNull(listCaddie.get(i).customerNames)) {
            LatLon bean;
            for (int x = 0; x < listCaddie.get(i).customerNames.size(); x++) {
              bean = new LatLon();
              bean.userName = listCaddie.get(i).customerNames.get(x);
              listCaddieWithPlayer.add(bean);
            }
          }
          if (Util.listIsNull(listCaddieWithPlayer)) {
            continue;
          }
          if (map.containsKey(wayName)
              && null != map.get(wayName)) {
            map.get(wayName).add(listCaddieWithPlayer);
          } else {
            listData = new ArrayList<>();
            listData.add(listCaddieWithPlayer);
            map.put(wayName, listData);
          }
          break;
        } else {
          if (j == listAll.size() - 1) {
            wayName = "不在球道内";
            listCaddieWithPlayer.add(listCaddie.get(i));
            if (!Util.listIsNull(listCaddie.get(i).customerNames)) {
              LatLon bean;
              for (int x = 0; x < listCaddie.get(i).customerNames.size(); x++) {
                bean = new LatLon();
                bean.userName = listCaddie.get(i).customerNames.get(x);
                listCaddieWithPlayer.add(bean);
              }
            }
            if (Util.listIsNull(listCaddieWithPlayer)) {
              continue;
            }
            if (map.containsKey(wayName)
                && null != map.get(wayName)) {
              map.get(wayName).add(listCaddieWithPlayer);
            } else {
              listData = new ArrayList<>();
              listData.add(listCaddieWithPlayer);
              map.put(wayName, listData);
            }
          }
        }
      }
    }
    if (!CollectionUtil.mapIsNull(map)) {
      PlayerBean bean;
      for (Map.Entry<String, List<List<LatLon>>> me : map.entrySet()) {
        int num = 0;
        if (!Util.listIsNull(me.getValue())) {
          for (int i = 0; i < me.getValue().size(); i++) {
            if (!Util.listIsNull(me.getValue().get(i))) {
              num += me.getValue().get(i).size();
            }
          }
        }
        bean = new PlayerBean.Builder()
            .setFairwayName(me.getKey())
            .setPersonNum(num)
            .setListAllPerson(me.getValue())
            .build();
        listInWayBack.add(bean);
      }
    }
    EventBus.getDefault().post(new EventPark.Builder()
        .setTarget(DataManager.STATISTICS_PLAYER_IN_PARK_PERSON_LIST)
        .setListPlayers(listInWayBack)
        .build());
  }

  private void filterDataBegin2(List<LatLon> listC, List<ParkFair> listAll) {
    List<LatLng> listCaddieLatlng = MapUtil.getCovertList(listC);
    List<List<LatLng>> listAllLatlng = MapUtil.geListAllLatlng(listAll);
    List<LatLon> listCaddieWithPlayer;
    List<List<LatLon>> listData;
    Map<String, List<List<LatLon>>> map = new ArrayMap<>();
    String wayName;
    List<LatLon> listCaddie = new ArrayList<>();
    listCaddie.addAll(listC);
    for (int allIndex = 0; allIndex < listAll.size(); allIndex++) {
      wayName = listAll.get(allIndex).fairwayName;
      listCaddieWithPlayer = new ArrayList<>();
      if (!Util.listIsNull(listCaddie)) {
        for (int caddieIndex = 0; caddieIndex < listCaddie.size(); caddieIndex++) {
          if (MapUtil.isInRange(listCaddieLatlng.get(caddieIndex), listAllLatlng.get(allIndex))) {
            listCaddieWithPlayer.add(listCaddie.get(caddieIndex));
            if (!Util.listIsNull(listCaddie.get(caddieIndex).customerNames)) {
              LatLon bean;
              for (int x = 0; x < listCaddie.get(caddieIndex).customerNames.size(); x++) {
                bean = new LatLon();
                bean.userName = listCaddie.get(caddieIndex).customerNames.get(x);
                listCaddieWithPlayer.add(bean);
              }
            }
            listCaddie.remove(caddieIndex);
          }
        }
      }
      if (map.containsKey(wayName)
          && null != map.get(wayName)) {
        map.get(wayName).add(listCaddieWithPlayer);
      } else {
        listData = new ArrayList<>();
        listData.add(listCaddieWithPlayer);
        map.put(wayName, listData);
      }
      if (!Util.listIsNull(listCaddie)) {
        for (int i = 0; i < listCaddie.size(); i++) {
          listCaddieWithPlayer = new ArrayList<>();
          listCaddieWithPlayer.add(listCaddie.get(i));
          if (!Util.listIsNull(listCaddie.get(i).customerNames)) {
            LatLon bean;
            for (int x = 0; x < listCaddie.get(i).customerNames.size(); x++) {
              bean = new LatLon();
              bean.userName = listCaddie.get(i).customerNames.get(x);
              listCaddieWithPlayer.add(bean);
            }
          }
        }
        listData = new ArrayList<>();
        listData.add(listCaddieWithPlayer);
        map.put("不在球道内", listData);
      }
    }
    if (!CollectionUtil.mapIsNull(map)) {
      PlayerBean bean;
      for (Map.Entry<String, List<List<LatLon>>> me : map.entrySet()) {
        int num = 0;
        if (!Util.listIsNull(me.getValue())) {
          for (int i = 0; i < me.getValue().size(); i++) {
            if (!Util.listIsNull(me.getValue().get(i))) {
              num += me.getValue().get(i).size();
            }
          }
        }
        bean = new PlayerBean.Builder()
            .setFairwayName(me.getKey())
            .setPersonNum(num)
            .setListAllPerson(me.getValue())
            .build();
        listInWayBack.add(bean);
      }
    }
    EventBus.getDefault().post(new EventPark.Builder()
        .setTarget(DataManager.STATISTICS_PLAYER_IN_PARK_PERSON_LIST)
        .setListPlayers(listInWayBack)
        .build());
  }

}
